﻿using AJWPFAdmin.Core.Utils;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace AJWPFAdmin.Core.Components.AJFilePicker.ViewModels
{
    public class UploadFileItem : BindableBase
    {
        private string _url;
        public string Url
        {
            get { return _url; }
            set { SetProperty(ref _url, value); }
        }

        private Visibility _loading;
        public Visibility Loading
        {
            get { return _loading; }
            set { SetProperty(ref _loading, value); }
        }

        private Visibility _canDelete;
        public Visibility CanDelete
        {
            get { return _canDelete; }
            set { SetProperty(ref _canDelete, value); }
        }


        private double _displayHeight;
        public double DisplayHeight
        {
            get { return _displayHeight; }
            set { SetProperty(ref _displayHeight, value); }
        }

        private double _displayWidth;
        public double DisplayWidth
        {
            get { return _displayWidth; }
            set { SetProperty(ref _displayWidth, value); }
        }

        private Image _imgCtrl;
        public Image Image
        {
            get { return _imgCtrl; }
            private set { _imgCtrl = value; }
        }

        private Stream _stream;
        private DispatcherTimer _timer;

        private DelegateCommand<object> _imageCtrlLoadedCmd;
        public DelegateCommand<object> ImageCtrlLoadedCmd =>
            _imageCtrlLoadedCmd ?? (_imageCtrlLoadedCmd = new DelegateCommand<object>(ExecuteImageCtrlLoadedCmd));

        void ExecuteImageCtrlLoadedCmd(object parameter)
        {
            if (parameter is Image img)
            {
                Image = img;
                Loading = Visibility.Visible;
                if (!string.IsNullOrWhiteSpace(_url))
                {
                    var worker = new BackgroundWorker();
                    worker.DoWork += (_, e) =>
                    {
                        var stream = CommonUtil.GetImageStreamFromLocalOrHttp(_url, out var __);
                        if (stream == null)
                        {
                            stream = CommonUtil.GetImageStreamFromLocalOrHttp(Path.Combine(AppContext.BaseDirectory, "Images\\media-empty.png"), out __);
                        }
                        e.Result = stream;
                    };
                    worker.RunWorkerCompleted += (_, e) =>
                    {
                        if (e.Error != null || e.Result == null)
                        {
                            Loading = Visibility.Collapsed;
                            return;
                        }

                        _stream = e.Result as Stream;

                    };
                    worker.RunWorkerAsync();

                    var timerCount = 0;
                    _timer = new DispatcherTimer
                    {
                        Interval = TimeSpan.FromSeconds(1)
                    };
                    _timer.Tick += (_, __) =>
                    {
                        if (timerCount > 10)
                        {
                            Loading = Visibility.Collapsed;
                            _timer.Stop();
                            return;
                        }
                        if (_stream != null)
                        {
                            Loading = Visibility.Collapsed;
                            _timer.Stop();
                            Image.Source = CommonUtil.CreateBitmapImageFromStream(_stream);
                        }
                        else
                        {
                            timerCount++;
                        }
                    };
                    _timer.Start();
                }

            }
        }

        private DelegateCommand<object> _imageCtrlUnloadedCmd;
        public DelegateCommand<object> ImageCtrlUnloadedCmd =>
            _imageCtrlUnloadedCmd ?? (_imageCtrlUnloadedCmd = new DelegateCommand<object>(ExecuteImageCtrlUnloadedCmd));

        void ExecuteImageCtrlUnloadedCmd(object parameter)
        {
            if (parameter is Image img)
            {
                if (!string.IsNullOrWhiteSpace(_url))
                {
                    _timer?.Stop();
                }
            }
        }

        public UploadFileItem(string url)
        {
            Loading = !string.IsNullOrWhiteSpace(_url) ? Visibility.Visible : Visibility.Collapsed;
            Url = url;

        }

        //public void InitImage()
        //{
        //    if (!string.IsNullOrWhiteSpace(_url))
        //    {
        //        var stream = CommonUtil.GetImageStreamFromLocalOrHttp(_url);

        //        Image = CommonUtil.CreateBitmapImageFromStream(stream);
        //    }
        //}
    }
}
